package com.xiaoyu.lanling.feature.view.weight.picker.single;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.FrameLayout;
import android.widget.NumberPicker;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xiaoyu.base.utils.ListUtil;
import com.xiaoyu.lanling.R;
import com.xiaoyu.lanling.feature.view.weight.picker.PickerModel;

import java.util.ArrayList;
import java.util.List;

public class PickerSingleView extends FrameLayout implements NumberPicker.OnValueChangeListener {

    private NumberPicker mPicker;

    private List<PickerModel> models;
    private PickerModel model;

    public PickerSingleView(@NonNull Context context) {
        this(context, null);
    }

    public PickerSingleView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PickerSingleView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public PickerModel getModel() {
        return model;
    }

    public void scrollToPosition(String key) {
        try {
            if (TextUtils.isEmpty(key)) {
                return;
            }

            if (ListUtil.isNullOrEmpty(models)) {
                return;
            }
            for (int i = 0; i < models.size(); i++) {
                final String firstKey = models.get(i).key;
                if (TextUtils.equals(firstKey, key)) {
                    mPicker.setValue(i + 1);
                    model = models.get(i);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void scrollToPositionByValue(String value) {
        try {
            if (TextUtils.isEmpty(value)) {
                return;
            }

            if (ListUtil.isNullOrEmpty(models)) {
                return;
            }
            for (int i = 0; i < models.size(); i++) {
                final String firstValue = models.get(i).value;
                if (TextUtils.equals(firstValue, value)) {
                    mPicker.setValue(i + 1);
                    model = models.get(i);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressLint("InflateParams")
    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_picker_single, null);
        mPicker = view.findViewById(R.id.picker);

        mPicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
        mPicker.setOnValueChangedListener(this);

        addView(view);
    }

    public void initData(List<PickerModel> keyList) {
        if (ListUtil.isNullOrEmpty(keyList)) {
            return;
        }
        models = keyList;
        model = keyList.get(0);
        processData();
    }

    private void processData() {
        setData(models);
    }

    private void setData(List<PickerModel> models) {
        List<String> valueList = getValueListFromKeyList(models);
        if (ListUtil.isNullOrEmpty(valueList)) {
            return;
        }
        mPicker.setDisplayedValues(null); // need clean
        mPicker.setMinValue(1);
        mPicker.setMaxValue(valueList.size());
        mPicker.setWrapSelectorWheel(false);
        mPicker.setDisplayedValues(valueList.toArray(new String[]{}));
        mPicker.setValue(1);
    }

    private List<String> getValueListFromKeyList(List<PickerModel> models) {
        List<String> valueList = new ArrayList<>();
        for (PickerModel model : models) {
            valueList.add(model.value);
        }
        return valueList;
    }

    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        switch (picker.getId()) {
            case R.id.picker:
                model = models.get(newVal - 1);
                break;
        }
    }
}
